<template>
  <div class="login-box">
    <h3 class="p-title">发票打印助手</h3>
    <div class="login-bg">
      <img :src="bg" class="bgImg">
    </div>
    <div class="login-page-container">
      <el-form
        :model="ruleForm2"
        :rules="rules2"
        ref="ruleForm2"
        label-position="left"
        label-width="0px"
        class="demo-ruleForm login-container"
      >
        <div class="register">
          <div class="register-title" v-if="type == '1'">欢迎注册</div>
          <div class="register-title" v-if="type == '2'">首次登录</div>
          <div class="el-link register-content">
            已有账号？
          </div>
          <el-link type="primary" @click="goLogin">登录</el-link>
        </div>

        <el-form-item prop="account">
          <div class="form-item-title">用户名</div>
          <el-input type="text" clearable style="width: 78%" v-model="ruleForm2.account" placeholder="请设置用户名"></el-input>
        </el-form-item>
        <el-form-item prop="checkPass">
          <div class="form-item-title">密码</div>
          <el-input type="password" show-password style="width: 78%" v-model="ruleForm2.checkPass" placeholder="请设置登录密码"></el-input>
        </el-form-item>
<!--        <el-form-item prop="confimPass">-->
<!--          <div class="form-item-title">确认密码</div>-->
<!--          <el-input type="password" style="width: 78%" v-model="ruleForm2.confimPass" placeholder="再次输入密码"></el-input>-->
<!--        </el-form-item>-->
        <div v-if="type == '1'">
          <el-form-item prop="phone">
            <div class="form-item-title">手机号</div>
            <el-input type="text" clearable style="width: 78%" v-model="ruleForm2.phone" placeholder="可用于登录和找回密码"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div class="form-item-title">验证码</div>
            <el-input type="text" clearable v-model="ruleForm2.code" placeholder="请输入验证码" style="width: 45%"></el-input>
            <a class="el-link get-code" v-show="verShow" v-loading="phoneLoading" @click="getPhoneCode">获取验证码</a>
            <span class="get-code" v-show="!verShow">{{timer}}秒后重新获取</span>
          </el-form-item>
        </div>

        <el-form-item style="width:100%;margin-bottom: 10px;">
          <el-button v-if="type == '1'" type="primary" style="width:100%;" class="login-btn" @click="register" :loading="logining">注册</el-button>
          <el-button v-if="type == '2'" type="primary" style="width:100%;" class="login-btn" @click="register" :loading="logining">保存</el-button>
        </el-form-item>
        <div class="agreement" v-if="type == '1'">
          <el-checkbox click="remberuser" v-model="checked" class="el-link remember">以阅读并同意</el-checkbox>
          <el-link type="primary" @click="invoiceContent">发票授权协议</el-link>
        </div>
        <div class="agreement" style="visibility:hidden;" v-else>
          <el-checkbox click="remberuser" v-model="checked" class="el-link remember">以阅读并同意</el-checkbox>
          <el-link type="primary" @click="invoiceContent">发票授权协议</el-link>
        </div>
        <div class="error" :style="{visibility:!error?'hidden':'visible'}">{{errorText}}</div>
      </el-form>
    </div>

    <el-dialog title="发票授权协议" :visible.sync="invoiceDialogVisible" :modal-append-to-body='false' :center="true">
      <div v-html="protocolInfo" style="height:600px;overflow: auto;">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as commonApi from "api/common";
import bg from '../assets/img/login-bg1.png'
export default {
  data() {
    return {
      invoiceDialogVisible: false, //协议展示框
      protocolInfo: '',
      bg: bg, //logo
      phoneLoading: false, //获取验证码加载
      activeName: '1', //登录方式
      logining: false, //登录加载
      ruleForm2: { // form表单值
        account: "",
        checkPass: "",
        // confimPass: "",
        phone: "",
        code: "",
      },
      rules2: { // form 表单验证
        account: [
          { required: true, message: '请输入用户名', trigger: 'change' },
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'change' },
        ],
        // confimPass: [
        //   {validator: ((rule, value, callback) => {
        //       if (value === '') {
        //         callback(new Error('请再次输入密码'))
        //       } else if (value != this.ruleForm2.checkPass) {
        //         callback(new Error('两次密码不一致'))
        //       } else {
        //         callback()
        //       }
        //     }), trigger: 'change' }
        // ],
        phone: [
          {validator: ((rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入手机号码'))
              } else if (!this.checkMobile(value)) {
                callback(new Error('手机号码不合法'))
              } else {
                callback()
              }
            }), trigger: 'change' }
        ],
        code: [
          { validator: ((rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入验证码'))
              } else {
                callback()
              }
            }), trigger: 'change' },
        ],
      },
      error: false, //错误提示状态
      errorText: '错误',//错误提示内容
      checked: false, //协议勾选
      verShow: true, //验证码显示控制
      timer: 60, //验证码重新获取倒计时
      type: '1', //注册类型 1默认 2不需手机以及验证码
    };
  },
  created() {
    // 初始打开模块
    let type = this.$route.params.type
    if (type != null && type != ''){ //指定模块
      this.type = type
      this.ruleForm2.phone = this.$route.params.phone
    }
    this.getInvoiceProtocolInfo()
  },
  methods: {
    register() {
      this.$refs.ruleForm2.validate(valid => {
        if (valid) {
          if (this.type == '1' && !this.checked){
            this.showError('请阅读同意协议')
            return
          }
          this.logining = true;
          const params = {
            loginType: this.type,
            name: this.ruleForm2.account,
            password: this.ruleForm2.checkPass,
            phone: this.ruleForm2.phone,
            code: this.ruleForm2.code,
          };
          // 发送API请求
          commonApi.registerUser(params).then(res => {
            let { data } = res;
            this.logining = false;
            if (data.status === 200) {
              sessionStorage.setItem("user", JSON.stringify(data.data)); // session存储用户信息
              if (this.type == '1'){
                this.$notify({
                  title: '注册成功',
                  message: '即将自动登录',
                  type: 'success',
                  duration: 900
                });
                setTimeout(() =>{
                  this.$router.push({ path: "/guide" });  // 去引导页
                },1000)
              } else {
                setTimeout(() =>{
                  this.$router.push({ path: "/guide" });  // 去引导页
                },500)
              }
            } else {
              this.showError(data.msg)
            }
          })
          .catch(() => {
            this.logining = false;
            this.showError("对不起,连接服务器异常,请稍后再试!")
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    goLogin(){
      this.$router.push({ path: "/login" });  // 去登录
    },
    invoiceContent(){ // 查看协议内容
      if (this.protocolInfo === null || this.protocolInfo === '') {
        this.getInvoiceProtocolInfo()
      }
      this.invoiceDialogVisible = true
    },
    getPhoneCode(){ // 获取手机验证码
      let phone = this.ruleForm2.phone
      this.$refs.ruleForm2.validateField('phone' ,valid => { //单个字段检验
        if(!valid){
          this.phoneLoading = true
          commonApi.sendPhoneCode(phone, 2).then(res => {
            let { data } = res;
            this.phoneLoading = false
            if (data.status === 200) {
              this.verShow = false
              var auth_timer = setInterval(()=>{ //倒计时
                this.timer--
                if(this.timer<=0){
                  this.verShow = true
                  this.timer = 60
                  clearInterval(auth_timer)
                }
              },1000)
            } else {
              this.showError(data.msg)
            }
          })
        }
      })
    },
    // 验证手机号
    checkMobile(str) {
      let re = /^1\d{10}$/
      if (re.test(str)) {
        return true;
      } else {
        return false;
      }
    },
    getInvoiceProtocolInfo(){ //获取协议内容
      commonApi.findProtocolInfo(1).then(res => {
        let { data } = res;
        if (data.status === 200) {
          this.protocolInfo = data.data
        }
      })
    },
    showError(t){ //展示错误
      this.errorText = t
      this.error = true
      setTimeout(()=>{
        this.error = false
      }, 5000)
    },
  }
};
</script>

<!--组件样式 本页面修改-->
<style scoped lang="scss">
  @import "../assets/css/them.scss";
  .login-container{
    /deep/ .el-input__inner{
      height: 32px;
      line-height: 32px;
      border-radius: 6px;
    }
    /deep/ .el-form-item__error{
      left: 75px;
    }
  }
  .remember{
    /deep/ .el-checkbox__inner{
      border-radius: 10px;
    }
  }
</style>
<style scoped lang="scss">
@import "../assets/css/them.scss";

@media screen and (max-width: 1200px) {
  .login-box {
    width: 100%;
    height: 100%;
    min-width: 900px;
    position: fixed;
    left: 0;
    top: 0;
    background-color: $globalBgColor;
    background: url("../assets/img/login-bg.png") right no-repeat;
    background-size: 68% 100%;
    .p-title {
      display: inline-block;
      padding: 1% 3%;
      position: absolute;
      font-size: 30px;
    }
    .login-bg{
      position: absolute;
      z-index: -99;
      display: inline-block;
      padding-top: 12%;
      padding-left: 1%;
      .bgImg{
        padding-left: 50px;
        width: 65%;
      }
    }
    .login-page-container{
      display: inline-block;
      float:right;
      padding-right: 6%;
      margin-top: 5vh;
      .login-container {
        -webkit-border-radius: 20px;
        border-radius: 20px;
        -moz-border-radius: 20px;
        background-clip: border-box;
        width: 300px;
        padding: 20px 30px;
        background: $baseBgColor;
        border: 1px solid #eaeaea;
        .register{
          padding: 10px 20px 20px 20px;
          .register-title{
            font-size: 26px;
            font-weight: 700;
          }
          .register-content {
            display: inline-block;
          }
        }
        .form-item-title{
          display: inline-block;
          width: 20%;
          text-align: left;
          color: #333333;
          font-size: 14px;
          font-weight: 400;
        }
        .get-code{
          padding-left: 5px;
          color: #3B83FB;
          font-weight: 400;
          font-size: 12px;
          text-decoration: none;
        }
        .login-btn{
          margin-top: 20px;
          border-radius: 20px;
        }
      }
      .agreement{
        padding-bottom: 5px;
      }
      .error{
        text-align: center;
        color: red;
        font-size: 12px;
      }
    }
  }
}
@media screen and (min-width: 1200px) and (max-width: 1600px){
  .login-box {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: $globalBgColor;
    background: url("../assets/img/login-bg.png") right no-repeat;
    background-size: 68% 100%;
    .p-title {
      display: inline-block;
      padding: 2% 5%;
      position: absolute;
      font-size: 30px;
    }
    .login-bg{
      position: absolute;
      z-index: -99;
      display: inline-block;
      padding-top: 10%;
      padding-left: 5%;
      .bgImg{
        padding-left: 50px;
      }
    }
    .login-page-container{
      display: inline-block;
      float:right;
      padding-right: 10%;
      margin-top: 10vh;
      .login-container {
        -webkit-border-radius: 20px;
        border-radius: 20px;
        -moz-border-radius: 20px;
        background-clip: border-box;
        width: 300px;
        padding: 20px 30px;
        background: $baseBgColor;
        border: 1px solid #eaeaea;
        .register{
          padding: 20px 20px 30px 20px;
          .register-title{
            font-size: 26px;
            font-weight: 700;
          }
          .register-content {
            display: inline-block;
          }
        }
        .form-item-title{
          display: inline-block;
          width: 20%;
          text-align: left;
          color: #333333;
          font-size: 14px;
          font-weight: 400;
        }
        .get-code{
          padding-left: 5px;
          color: #3B83FB;
          font-weight: 400;
          font-size: 12px;
          text-decoration: none;
        }
        .login-btn{
          margin-top: 20px;
          border-radius: 20px;
        }
      }
      .agreement{
        padding-bottom: 10px;
      }
      .error{
        text-align: center;
        color: red;
        font-size: 12px;
      }
    }
  }
}
@media screen and (min-width: 1600px) {
  .login-box {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background-color: $globalBgColor;
    background: url("../assets/img/login-bg.png") right no-repeat;
    background-size: 68% 100%;
    .p-title {
      display: inline-block;
      padding: 2% 5%;
      position: absolute;
      font-size: 36px;
    }
    .login-bg{
      position: absolute;
      z-index: -99;
      display: inline-block;
      padding-top: 12%;
      padding-left: 10%;
      .bgImg{
        padding-left: 50px;
      }
    }
    .login-page-container{
      display: inline-block;
      float:right;
      padding-right: 10%;
      margin-top: 10%;
      .login-container {
        -webkit-border-radius: 20px;
        border-radius: 20px;
        -moz-border-radius: 20px;
        background-clip: border-box;
        width: 300px;
        padding: 35px 35px 15px;
        background: $baseBgColor;
        border: 1px solid #eaeaea;
        .register{
          padding: 20px 20px 40px 0;
          .register-title{
            font-size: 26px;
            font-weight: 700;
          }
          .register-content {
            display: inline-block;
          }
        }
        .form-item-title{
          display: inline-block;
          width: 20%;
          text-align: left;
          color: #333333;
          font-size: 14px;
          font-weight: 400;
        }
        .get-code{
          padding-left: 5px;
          color: #3B83FB;
          font-weight: 400;
          font-size: 12px;
          text-decoration: none;
        }
        .login-btn{
          margin-top: 20px;
          border-radius: 20px;
        }
      }
      .agreement{
        padding-bottom: 15px;
      }
      .error{
        text-align: center;
        color: red;
        font-size: 12px;
      }
    }
  }
}



</style>


